import request from '@/utils/request'

// 获取菜单列表
export function getMenuList(params) {
  return request({
    url: '/menu/list',
    method: 'get',
    params
  }).then(res => {
    if (res.status) {
      res.data.forEach(item => {
        if (item.parentPermit === item.permitTitle) {
          console.log(item.parentPermit, item.permitTitle, item, '父级权限不能和子权限名称相同')
        }
        item.parentPermit = item.parentPermit || ''
      })
    }
    return res
  })
}

// 更新菜单
export function updateMenu(params) {
  return request({
    url: '/menu/update',
    method: 'put',
    data: params
  })
}

// 添加菜单
export function addMenu(params) {
  return request({
    url: '/menu/add',
    method: 'put',
    data: params
  })
}

// 更新菜单显示状态
export function setMenuIsHidden(params) {
  return request({
    url: '/menu/setIsHidden',
    method: 'put',
    data: params
  })
}

// 更新菜单单字段值
export function setMenuKeyValue(row, field) {
  return request({
    url: '/menu/setKeyValue',
    method: 'put',
    data: { resId: row.resId, [field]: row[field] }
  })
}
// 删除菜单
export function deleteMenu(resId) {
  return request({
    url: `/menu/${resId}`,
    method: 'delete'
  })
}
